﻿#include "reservehotel.h"
#include "ui_reservehotel.h"
#include <QMessageBox>

ReserveHotel::ReserveHotel(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ReserveHotel)
{
    ui->setupUi(this);

    ui->pb_finish->setEnabled(false);
    ui->pb_finish->setStyleSheet("background-color: rgb(107, 107, 107);");
}

ReserveHotel::~ReserveHotel()
{
    delete ui;
}

void ReserveHotel::setReserveTime(const QString& inTime, const QString& outTime)
{
    m_reserveInfo.setCheckInTime(inTime);
    m_reserveInfo.setCheckOutTime(outTime);

    QString showStr = inTime;
    QString day;
    QDate inTimeDate = QDate::fromString(inTime,"yyyy-MM-dd");
    QDate outTimeDate = QDate::fromString(outTime,"yyyy-MM-dd");

    day = "星期" + GlobalVars::week[inTimeDate.dayOfWeek()-1];
    if (inTimeDate == QDate::currentDate())
    {
        showStr += "今天";
    }else
    {
        showStr += day;
    }

    showStr += ("  至  " + outTime + " ");
    day = "星期" + GlobalVars::week[outTimeDate.dayOfWeek()-1];
    showStr += day;

    int count = MyDate::DaysBetween2Date(inTime,outTime);
    showStr += ("  共" + QString::number(count) + "晚");
    ui->lb_inOutTime->setText(showStr);
}

void ReserveHotel::setHotel(const HotelInfo& hotelInfo)
{
    m_reserveInfo.setHotel(hotelInfo.getHotel());
    m_reserveInfo.setHotelName(hotelInfo.getName());
    m_reserveInfo.setHotelLocation(hotelInfo.getLocation());
    m_reserveInfo.setHotelPhone(hotelInfo.getPhone());

    ui->lb_hotelName->setText(hotelInfo.getName());
    ui->lb_hotelLocation->setText(hotelInfo.getLocation());
    ui->lb_cancel->setText(hotelInfo.getCancellation());
}

void ReserveHotel::setHouse(const HouseInfo& houseInfo)
{
    m_reserveInfo.setHouseType(houseInfo.getType());
    m_reserveInfo.setPayment(houseInfo.getPrice());

    ui->lb_houseType->setText(houseInfo.getType());
    ui->lb_area->setText("面积 " + houseInfo.getArea());
    ui->lb_bathroomAcc->setText("浴室配套 " + houseInfo.getBathroomAcc());
    ui->lb_price->setText("¥" + QString::number(houseInfo.getPrice()));
    ui->lb_capacity->setText("可住 " + QString::number(houseInfo.getCapacity()) + "人");
    ui->lb_bedType->setText("床型 " + houseInfo.getBedType());
    ui->lb_window->setText("窗户 " + houseInfo.getWindow());

    ui->lb_resHouseType->setText(houseInfo.getType());
//    ui->lb_breakfast->setText(houseInfo.getType());
//    ui->lb_media->setText(houseInfo.getType());
//    ui->lb_net->setText(houseInfo.get());
}

void ReserveHotel::submitReserveResult(bool result, const QString &msg)
{
    if (result)
    {
        QMessageBox::warning(this,
                     "成功",
                     msg,
                     QMessageBox::Ok,
                     QMessageBox::Ok);
        this->close();
    }
    else
    {
        QMessageBox::warning(this,
                     "错误",
                     msg,
                     QMessageBox::Ok,
                     QMessageBox::Ok);
    }
}

void ReserveHotel::setImage(const QImage& image)
{
    ui->lb_image->setPixmap(QPixmap::fromImage(image.scaled(180,100)));
}

void ReserveHotel::on_cb_comeTime_currentIndexChanged(const QString &arg1)
{
    ui->cb_comeTime->setCurrentText(arg1);
}

void ReserveHotel::on_le_phone_editingFinished()
{
    QString arg1 = ui->le_phone->text();
    if (arg1.isEmpty())
    {
        ui->le_phone->setPlaceholderText("用于接收通知短信");
    }
    else
    {
        QRegExp password_reg_exp("^13[0-9]{1}[0-9]{8}|^15[0-9]{1}[0-9]{8}|^18[0-9]{1}[0-9]{8}");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            ui->le_phone->clear();
            ui->le_phone->setPlaceholderText("格式错误,重新输入");
        }
    }
}

void ReserveHotel::on_le_inManName_editingFinished()
{
    QString arg1 = ui->le_inManName->text();
    if (arg1.isEmpty())
    {
        ui->le_inManName->setPlaceholderText("请填写入住人姓名");
    }
    else
    {
        QRegExp password_reg_exp("^[\u4e00-\u9fa5]+$");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            ui->le_inManName->clear();
            ui->le_inManName->setPlaceholderText("格式错误,重新输入");
        }
    }
}

void ReserveHotel::on_le_inManName_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->le_inManName->setPlaceholderText("请填写入住人姓名");
        ui->lb_remindMan->setText(" ");
    }
    else
    {
        QRegExp password_reg_exp("^[\u4e00-\u9fa5]+$");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
             ui->lb_remindMan->setText("X");
             ui->pb_finish->setEnabled(false);
             ui->pb_finish->setStyleSheet("background-color: rgb(107, 107, 107);");
        }
        else
        {
             ui->lb_remindMan->setText("√");
             if (!ui->le_phone->text().isEmpty())
             {
                 ui->pb_finish->setEnabled(true);
                 ui->pb_finish->setStyleSheet("	background-color: #f60;");
             }
        }
    }
}

void ReserveHotel::on_le_phone_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->le_phone->setPlaceholderText("用于接收通知短信");
        ui->lb_remindPhone->setText(" ");
    }
    else
    {
        QRegExp password_reg_exp("^13[0-9]{1}[0-9]{8}|^15[0-9]{1}[0-9]{8}|^18[0-9]{1}[0-9]{8}");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            ui->lb_remindPhone->setText("X");
            ui->pb_finish->setEnabled(false);
            ui->pb_finish->setStyleSheet("background-color: rgb(107, 107, 107);");
        }
        else
        {
            ui->lb_remindPhone->setText("√");
            if (!ui->le_inManName->text().isEmpty())
            {
                ui->pb_finish->setEnabled(true);
                ui->pb_finish->setStyleSheet("	background-color: #f60;");
            }
        }
    }
}


void ReserveHotel::on_pb_finish_clicked()
{
    m_reserveInfo.setHouseCount(ui->cb_houseCount->currentText().toInt());
    m_reserveInfo.setCheckInName(ui->le_inManName->text());
    m_reserveInfo.setCheckInTel(ui->le_phone->text());
    m_reserveInfo.setReserveTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    m_reserveInfo.setArrivalTime(ui->cb_comeTime->currentText());
    m_reserveInfo.setReserve(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
    qDebug() << QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
    m_reserveInfo.setState("待付款");

    GlobalVars::g_localReserve = m_reserveInfo;
    emit signalSubmitReserve();
}

